package com.ruoyi.web.ydz.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.ydz.domain.ClinicalDisease;
import com.ruoyi.web.ydz.service.IClinicalDiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.ydz.mapper.ClinicalMapper;
import com.ruoyi.web.ydz.domain.Clinical;
import com.ruoyi.web.ydz.service.IClinicalService;

/**
 * 临床招募Service业务层处理
 * 
 * @author DRX
 * @date 2024-10-30
 */
@Service
public class ClinicalServiceImpl implements IClinicalService 
{
    @Autowired
    private ClinicalMapper clinicalMapper;
    @Autowired
    private IClinicalDiseaseService clinicalDiseaseService;

    /**
     * 查询临床招募
     * 
     * @param id 临床招募主键
     * @return 临床招募
     */
    @Override
    public Clinical selectClinicalById(Long id)
    {

        Clinical clinical = clinicalMapper.selectClinicalById(id);
        ClinicalDisease ClinicalDisease = new ClinicalDisease();
        ClinicalDisease.setClinicalId(id);
        List<ClinicalDisease> list = clinicalDiseaseService.selectClinicalDiseaseList(ClinicalDisease);
        clinical.setClinicalDiseaseList(list);
        /*
         * 如果当前用户身份不是管理员
         * 则无法查看未发布的详情
         *
         * */
        Boolean isAdmin = false;
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        if (!Objects.isNull(roles)) {
            isAdmin = roles.stream()
                    .anyMatch(bean -> bean.getRoleId().longValue() == 100) ? false : true;
        } else {
            isAdmin = false;
        }

        if(!isAdmin && "0".equals(clinical.getState())){
            return null;
        }

        return clinical;
    }

    /**
     * 查询临床招募列表
     * 
     * @param clinical 临床招募
     * @return 临床招募
     */
    @Override
    public List<Clinical> selectClinicalList(Clinical clinical)
    {
        return clinicalMapper.selectClinicalList(clinical);
    }

    /**
     * 查询临床招募列表
     *
     * @param clinical 临床招募
     * @return 临床招募
     */

    public List<Clinical> getClinicalList(Clinical clinical)
    {
        return clinicalMapper.getClinicalList(clinical);
    }

    /**
     * 新增临床招募
     * 
     * @param clinical 临床招募
     * @return 结果
     */
    @Override
    public int insertClinical(Clinical clinical)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        clinical.setCreateBy(user.getUserName());
        clinical.setCreateName(user.getNickName());
        clinical.setCreateTime(DateUtils.getNowDate());
        //删除疾病关联信息
        if(clinical.getDelClinicalDiseaseIds().length>0){
            clinicalDiseaseService.deleteClinicalDiseaseByIds(clinical.getDelClinicalDiseaseIds());
        }
        if(null != clinical.getClinicalDiseaseList() && clinical.getClinicalDiseaseList().size()>0) {
            clinical.getClinicalDiseaseList().forEach(item->{
                if(item.getId()!= null){
                    clinicalDiseaseService.updateClinicalDisease(item);
                }else{
                    item.setClinicalId(clinical.getId());
                    clinicalDiseaseService.insertClinicalDisease(item);
                }
            });
        }
        return clinicalMapper.insertClinical(clinical);
    }

    /**
     * 修改临床招募
     * 
     * @param clinical 临床招募
     * @return 结果
     */
    @Override
    public int updateClinical(Clinical clinical)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        clinical.setUpdateBy(user.getUserName());
        clinical.setUpdateName(user.getNickName());
        clinical.setUpdateTime(DateUtils.getNowDate());
        //删除疾病关联信息
        if(clinical.getDelClinicalDiseaseIds().length>0){
            clinicalDiseaseService.deleteClinicalDiseaseByIds(clinical.getDelClinicalDiseaseIds());
        }
        if(null != clinical.getClinicalDiseaseList() && clinical.getClinicalDiseaseList().size()>0) {
            clinical.getClinicalDiseaseList().forEach(item->{
                if(item.getId()!= null){
                    clinicalDiseaseService.updateClinicalDisease(item);
                }else{
                    item.setClinicalId(clinical.getId());
                    clinicalDiseaseService.insertClinicalDisease(item);
                }
            });
        }
        return clinicalMapper.updateClinical(clinical);
    }

    /**
     * 批量删除临床招募
     * 
     * @param ids 需要删除的临床招募主键
     * @return 结果
     */
    @Override
    public int deleteClinicalByIds(Long[] ids)
    {
        return clinicalMapper.deleteClinicalByIds(ids);
    }

    /**
     * 删除临床招募信息
     * 
     * @param id 临床招募主键
     * @return 结果
     */
    @Override
    public int deleteClinicalById(Long id)
    {
        return clinicalMapper.deleteClinicalById(id);
    }
}
